﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Borton.Data;

namespace Borton.Beans
{

    public enum FieldType
    {
        STRING,
        NUMBER,
        IDENTIFIER,
        MULTIIDENTIFIER
    }
    public class Field
    {
        public String columnName;
        public FieldType type;
        protected String value = null;
        protected Control control = null;

        public Field(String columnName, FieldType type)
        {
            this.columnName = columnName;
            this.type = type;
        }
        public Field(String columnName, FieldType type, String value) : this(columnName, type)
        {
            this.value = value;
        }
        public virtual String getValueForDisplay()
        {
            return value;
        }
        public virtual String getValueForSave()
        {
            return value;
        }
        public virtual void setValue(String value)
        {
            this.value = value;
            if (control != null)
            {
                control.Text = value;
            }
        }

        public virtual Control getControl(bool readOnly)
        {
            if (control == null)
            {
                control = new TextBox();
                control.Text = this.getValueForDisplay();
                control.Name = this.columnName + "Field";
                control.Size = new System.Drawing.Size(100, 20);
                control.Enabled = !readOnly;
            }
            return control; 
        }

        public virtual void saveState() {
            value = ((TextBox)control).Text;
        }
    }
    
    public abstract class Bean : ICloneable
    {
        
        protected String tableName;
        protected Field[] fields;
        protected Bean[] referringBeans = new Bean[0];

        public virtual String getTableName()
        {
            return tableName;
        }

        public virtual Field[] getFields()
        {
            return fields;
        }

        public virtual Bean[] getReferringBeans()
        {
            return referringBeans;
        }

        public virtual Field getField(String columnName)
        {
            for (int i = 0; i < fields.Length; i++ )
            {
                if (fields[i].columnName.Equals(columnName)) {
                    return fields[i];
                }
            }
            throw new Exception("Field " + columnName + " not found for table " + getTableName());
        }

        public virtual bool isNew()
        {
            return getId().getValueForSave() == null || getId().getValueForSave().Length == 0;
        }

        public virtual Field getId()
        {
            return getField("id");
        }

        public virtual String getDisplayName()
        {
            return fields[1].getValueForDisplay();
        }

        public virtual void saveState() {
            for (int i = 0; i < getFields().Length; i++)
            {
                getFields()[i].saveState();
            }
        }

        public virtual void delete()
        {
            DataAccess.delete_(this);
        }

        public virtual void load()
        {
            DataAccess.load_(this);
        }

        public virtual void save()
        {
            DataAccess.save_(this);
        }

        public virtual Bean[] getAll()
        {
            return DataAccess.getAll__(this);
        }

        public abstract object Clone();
    }
}
